/**
 * 剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
 * https://leetcode-cn.com/problems/diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof/
 */
const { displayBaseArray } = require('../../libs/public');

/**
 * @param {number[]} nums
 * @return {number[]}
 */
function exchange(nums) {
  let left = 0;
  let right = nums.length - 1;
  while (left < right) {
    while (left < right && nums[left] % 2 === 1) {
      left += 1;
    }
    while (left < right && nums[right] % 2 === 0) {
      right -= 1;
    }
    if (left < right) {
      const temp = nums[left];
      nums[left] = nums[right];
      nums[right] = temp;
      left += 1;
      right -= 1;
    }
  }
  return nums;
}

console.log(displayBaseArray(exchange([1, 2, 3, 4])) === '[1,3,2,4]');
